#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int T;
  cin >> T;
  while (T--) {
    ll C, D;
    cin >> C >> D;
    ll ans = 0, base = 1;
    //  枚举 C +x 的位数
    for (int d = 0; d <= 18; ++d) {
      ll L = max(base - C, 1ll), R = min(base * 10 - 1 - C, D);
      if (L <= R)
        ans += (int)sqrtl(R + C * (base * 10 + 1)) -
               (int)sqrtl(L + C * (base * 10 + 1) - 1);
      base *= 10;
    }
    cout << ans << endl;
  }
  return 0;
}